<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class model_admin extends CI_Model{
	
	function isi_combo($p1='',$p2=''){
		switch($p1){
			case 'artikel':
				$sql = "SELECT * FROM tb_category";
			break;
		}
		
		return $this->db->query($sql)->result_array();			
	}
	
	function getData($table=""){
		if($table == "view_posting"){
			if($this->session->userdata('accesible') == 1){
				$sql = "SELECT * FROM ".$table." ";
			}else{
				$sql = "SELECT * FROM ".$table." WHERE username = '".$this->session->userdata('username')."'";
			}
		}elseif($table == 'tb_category2'){
				$sql = "SELECT * FROM tb_category";
		}else{
			$sql = "SELECT * FROM ".$table." ";
		}
		//print_r( $this->db->query($sql)->result_array());exit;
		if($table == 'tb_category2'){
			return $this->db->query($sql)->result_array();
		}else{
			return $this->get_json($sql, $table);
		}
	}
	
	function getRecord($table="", $field="", $id=""){
		$querySQL = $this->db->query("SELECT * FROM ".$table." WHERE ".$field." = '".$id."' ")->row();
		
		return $querySQL;
	}
	
	function saveDataModel($post, $table="", $editstatus=""){
		//unset($post['stateid']);
		
		if(!empty($_FILES['picture']['name'])){	
			$ext = explode('.',$_FILES['picture']['name']);
			$exttemp = sizeof($ext) - 1;
			$extension = $ext[$exttemp];
			$md5_foto = md5($_FILES['picture']['name']);
			if($table == "tb_posting"){
				$upload_path = "./assets/uploads/artikel/";
				$nama_foto =  'artikel_'.$md5_foto.'.'.$extension;
			}elseif($table == "tb_gallery_picture"){
				$upload_path = "./assets/uploads/galeri/";
				$nama_foto =  'gallery_'.$md5_foto.'.'.$extension;
			}
			$post['picture'] =  $nama_foto;
		}
		
		if($table == "tb_posting"){
			$post['datecreate'] =  date('Y-m-d');
			$post['username'] =  $this->session->userdata('username');
			$post['permalink'] = toAscii($post['post_title']); //str_replace(" ", "-", strtolower($post['post_title']));
			$post['publish_status'] = "N";
			$post['viewcount'] =  0;
		}
				
		$this->db->trans_begin();
		
			if($editstatus == "new"){
				$this->db->insert($table, $post);
			}elseif($editstatus == "edit"){
				
				if(!empty($_FILES['picture']['name'])){	
					$sqlPict = $this->db->query("SELECT * FROM ".$table." WHERE stateid = '".$post['stateid']."' ")->row();
					if($sqlPict->picture != ""){ 
						$nama_update_file = $sqlPict->picture;
						if(file_exists($upload_path.$nama_update_file)){
							unlink($upload_path.$nama_update_file);
						}
					}
				}	
				$this->db->update($table, $post, array('stateid'=>$post['stateid']));
			}
			
				if(!empty($_FILES['picture']['name'])){	
					$file = $_FILES['picture']['name'];
					$tmp  = $_FILES['picture']['tmp_name'];
						if(file_exists($upload_path.$nama_foto)){
							unlink($upload_path.$nama_foto);
							$uploadfile = $upload_path.$nama_foto;
						}	
						else{
							$uploadfile = $upload_path.$nama_foto;
						}
					move_uploaded_file($tmp, $uploadfile);
						if (!chmod($uploadfile, 0775)) {
							 echo "Gagal mengupload file";
							 exit;
						}
				}					
			
		if($this->db->trans_status() == false){
			$this->db->trans_rollback();
			return "Data not saved";
		} else{
			return $this->db->trans_commit();
		}
	}
	
	function get_json($sql="", $table=""){
		$page = (integer) (($this->input->post('page')) ? $this->input->post('page') : "1");
		$limit = (integer) (($this->input->post('rows')) ? $this->input->post('rows') : "10");
		 
		$count = $this->db->query($sql)->num_rows();
		
		if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if($start<0) $start=0;
		  
		$sql = $sql . " LIMIT $start,$limit";
	
		$data=$this->db->query($sql)->result_array();  
		
		if($table == "view_posting"){
			foreach($data as $k=>$v){
				$data[$k]['datecereate'] = tanggal_format_indonesia($v['datecreate']);
			}
		}
		
		if($data){
		   $responce = new stdClass();
		   $responce->rows= $data;
		   $responce->total =$count;
		   return json_encode($responce);
		}else{ 
		   $responce = new stdClass();
		   $responce->rows = array();
           $responce->total =0;
           echo json_encode($responce);
		} 
	}
	
	function deleteDataModel($param, $id="", $folder=""){
		$upload_path = "./assets/uploads/".$folder."/";
		$sql = $this->db->query("SELECT * FROM ".$param." WHERE stateid = '".$id."' ")->row();
		//$sql = new stdClass();
		if(isset($sql->picture)){
			if(file_exists($upload_path.$sql->picture)){
				unlink($upload_path.$sql->picture);
			}	
		}
		return $this->db->delete($param, array('stateid' => $id));
	}

	function countposting($parameter='',$category_code=''){
		if($parameter == 'postingsemua'){
			$sql = "select count(*) as jml from view_posting where category_code = '".$category_code."'";
		}elseif($parameter == 'postinganda'){
			$sql = "select count(*) as jml from view_posting where category_code = '".$category_code."' and username='".$this->session->userdata('username')."'";
		}
		
		$total = $this->db->query($sql)->row();
		return $total;
	}
	
}